<template>
  <el-card>
    <div slot="header">
      <!-- 面包屑 -->
      <div slot="header">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品管理</el-breadcrumb-item>
          <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <!-- 筛选区域 -->
    <div class="filter-bar">
      <!-- 搜素区域 -->
      <!-- 添加区域 -->
      <div class="r-btn">
        <el-button type="primary" size="small" @click="addDialogShow = true"
          >增加</el-button
        >
      </div>
    </div>
    <div class="table-box"></div>
    <!-- card body -->
    <!-- 分类表格
    :data(设置数据源) 
    :columns(设置表格中列配置信息) 
    :selection-type(是否有复选框)
    :expand-type(是否展开数据)
    show-index(是否设置索引列) 
    index-text(设置索引列头)
    border(是否添加纵向边框) 
    :show-row-hover(是否鼠标悬停高亮) -->
    <tree-table
      border
      :selection-type="false"
      :data="cateList"
      :columns="columns"
      :expand-type="false"
      :show-index="true"
    >
      <template slot="delete" slot-scope="scope">
        <i
          class="el-icon-success"
          v-if="scope.row.cat_deleted === false"
          style="color: lightgreen; font-size: 20px"
        ></i>
        <i class="el-icon-error" v-else style="color: red"></i>
      </template>
      <template slot="level" slot-scope="scope">
        <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
        <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1"
          >二级</el-tag
        >
        <el-tag size="mini" type="warning" v-else>三级</el-tag>
      </template>
      <template slot="action" slot-scope="scope">
        <el-button type="primary" size="small" icon="el-icon-edit"
          >编辑</el-button
        >
        <el-button
          type="warning"
          size="small"
          icon="el-icon-delete"
          @click="scope"
          circle
        ></el-button>
      </template>
    </tree-table>
    <!-- 页码结构 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-size="pagesize"
      :page-sizes="[1, 2, 3, 4, 5, 6, 10]"
      @size-change="sizeChange"
      @current-change="pageChange"
    >
    </el-pagination>
    <!-- 添加弹框 -->
    <el-dialog title="添加分类" :visible.sync="addDialogShow" width="30%">
      <el-form
        :model="addForm"
        ref="addForm"
        :rules="addFormRules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="分类名" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="上级分类" prop="cat_pid">
          <el-cascader
            :options="cateListAll"
            v-model="addForm.cat_pid"
            clearable
            :props="{
              value: 'cat_id',
              label: 'cat_name',
              checkStrictly: true,
            }"
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogShow = false">Cancel</el-button>
        <el-button type="primary" @click="addFormSubmit">OK</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
let defaultItem = {
  cat_pid: 0, // 父级iid
  cat_name: "", //父级分类
  cat_level: 0, //层级
};
export default {
  data() {
    return {
      cateList: [], // 分类列表
      cateListAll: [], // 全部分类列表
      pagenum: 1,
      pagesize: 10,
      total: 1,
      addDialogShow: false,
      addForm: { ...defaultItem },
      addFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名", trigger: "blur" },
        ],
      },
      //   表格中列配置信息
      columns: [
        { label: "分类名", prop: "cat_name" },
        {
          label: "是否有效",
          type: "template",
          prop: "cat_delete",
          template: "delete",
        },
        {
          label: "层级",
          type: "template",
          prop: "cat_level",
          template: "level",
        },
        { label: "操作", type: "template", template: "action" },
      ],
    };
  },
  //   发送数据
  created() {
    this.getCateList();
    this.getCateListAll();
  },
  methods: {
    //  获取分类数据
    async getCateList() {
      //   发送请求;
      let { data: res } = await this.$axios.get("categories", {
        //   携带参数
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        },
      });
      //   错误处理
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      //   储存数据到data
      this.total = res.data.total;
      this.cateList = res.data.result;
    },
    // 获取全部分类数据
    async getCateListAll() {
      //   发送请求;
      let { data: res } = await this.$axios.get("categories");
      //   错误处理
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.cateListAll = res.data;
    },
    // 切换每页数量
    sizeChange(val) {
      this.pagesize = val;
      this.getCateList();
    },
    // 切换页码
    pageChange(val) {
      this.pagenum = val;
      this.getCateList();
    },
    addFormSubmit() {
      this.$refs.addForm.validate(async (valid) => {
        if (!valid) return this.$message.error("表单格式不正确");
        // 处理pid
        // 判断是否为数组;
        if (Array.isArray(this.addForm.cat_pid)) {
          // 判断是否有长度
          if (this.addForm.cat_pid.length) {
            this.addForm.cat_level = this.addForm.cat_pid.length;
            this.addForm.cat_pid = this.addForm.cat_pid[
              this.addForm.cat_pid.length - 1
            ];
            console.log(this.addForm.cat_pid);
          } else {
            this.addForm.cat_pid = 0;
            this.addForm.cat_level = 0;
          }
        }
        let { data: res } = await this.$axios.post("categories", this.addForm);
        console.log(res);
        // 错误处理
        if (res.meta.status != 201) return this.$message.error(res.meta.msg);
        // 成功处理
        // 恢复addForm数据
        this.$message.success(res.meta.msg);
        this.addForm = { ...defaultItem };
        // 拉取数据
        this.getCateList();
        // 关闭弹窗
        this.addDialogShow = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>